home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 April / april_2001.iso / intercd / root / ^Palm / Games / eCross / src / levels / Builder.java next >
Encoding:
Java Source  |  2000-08-01  |  8.5 KB  |  343 lines

  1. // purpose: creates a Java source file for PalmOS which would create
  2. //          a database containing the game's levels. The sources file
  3. //          contains the levels, built from .bmp files
  4.  
  5. import java.io.*;
  6. import java.util.*;
  7.  
  8. import java.awt.*;
  9. import java.awt.image.*;
  10.  
  11. public class Builder
  12. {
  13.   private static final int NAME_SPACE = 12;
  14.   private static final String TEMPLATE = "LevelsMaker.template";
  15.   private static final String TEMPLATE_MATCH = "%TEMPLATE%";
  16.  
  17.   private final static Component component = new Component() { };
  18.   private final static MediaTracker tracker = new MediaTracker(component);
  19.  
  20.   private int width, height;
  21.   private ImageObserver imageObserver;
  22.  
  23.   Builder()
  24.   {
  25.     build();
  26.   }
  27.  
  28.   public static void main(String[] args)
  29.   {
  30.     new Builder();
  31.   }
  32.  
  33.   private void build()
  34.   {
  35.     try
  36.     {
  37.       // reads template file
  38.       File templateFile = new File(TEMPLATE);
  39.       StringBuffer template = new StringBuffer((int) templateFile.length());
  40.       BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(templateFile)));
  41.  
  42.       String line;
  43.       int pos = 0, index = -1;
  44.       boolean templateFound = false;
  45.  
  46.       for ( ; (line = in.readLine()) != null; )
  47.       {
  48.         if ((index = line.indexOf(TEMPLATE_MATCH)) != -1)
  49.         {
  50.           pos += index;
  51.           templateFound = true;
  52.           line = line.substring(0, index) + line.substring(index + TEMPLATE_MATCH.length());
  53.         } else if (!templateFound) {
  54.           pos += (line.length() + 1);
  55.         }
  56.  
  57.         template.append(line).append('\n');
  58.       }
  59.  
  60.       in.close();
  61.  
  62.       StringBuffer insert = new StringBuffer();
  63.       String files[] = getWildCardMatches(getUserDirectory() + File.separator + "datas", "*.gif");
  64.       sortStrings(files);
  65.  
  66.       for (int i = 0; i < files.length; i++)
  67.       {
  68.         Image image = Toolkit.getDefaultToolkit().getImage("datas" + File.separator + files[i]);
  69.         loadImage(image);
  70.         int[] bits = addTitle(files[i], getPixels(image));
  71.  
  72.         insert.append("\n    {\n     ");
  73.         insert.append(" 0, 1,\n     ");
  74.  
  75.         for (int j = 0; j < NAME_SPACE; j++)
  76.           insert.append(' ').append(bits[j] == -16777216 ? -1 : bits[j]).append(',');
  77.         insert.append("\n     ");
  78.  
  79.         for (int j = 0; j < height; j++)
  80.         {
  81.           for (int k = 0; k < width; k++)
  82.           {
  83.             insert.append(' ').append(handleSinglePixel(bits[(NAME_SPACE - 1) + j * width + k]));
  84.             if ((NAME_SPACE - 1) + j * width + k != bits.length - 1)
  85.               insert.append(',');
  86.           }
  87.  
  88.           if (j != height - 1)
  89.             insert.append("\n     ");
  90.         }
  91.  
  92.         insert.append("\n    }");
  93.         if (i != files.length - 1)
  94.           insert.append(',');
  95.       }
  96.  
  97.       template.insert(pos, insert.toString());
  98.  
  99.       String outName = TEMPLATE.substring(0, TEMPLATE.indexOf(".template")) + ".java";
  100.       PrintWriter out = new PrintWriter(new FileOutputStream(outName));
  101.       out.print(template.toString());
  102.       out.close();
  103.  
  104.     } catch (IOException ioe) { }
  105.  
  106.     System.exit(0);
  107.   }
  108.  
  109.   private int[] addTitle(String title, int[] bits)
  110.   {
  111.     int[] _bits = new int[bits.length + NAME_SPACE];
  112.  
  113.     int pos = 0;
  114.     for (int i = 0; i < title.length(); i++)
  115.     {
  116.       if (Character.isLetter(title.charAt(i)))
  117.       {
  118.         pos = i;
  119.         break;
  120.       }
  121.     }
  122.  
  123.     title = title.substring(pos, title.length() - 4);
  124.  
  125.     for (int i = 0; i < NAME_SPACE; i++)
  126.       _bits[i] = (i < title.length()) ? (int) title.charAt(i) : -1;
  127.  
  128.     System.arraycopy(bits, 0, _bits, NAME_SPACE - 1, bits.length);
  129.  
  130.     return _bits;
  131.   }
  132.  
  133.   private int handleSinglePixel(int pixel)
  134.   {
  135.     int alpha = (pixel >> 24) & 0xff;
  136.     int red   = (pixel >> 16) & 0xff;
  137.     int green = (pixel >>  8) & 0xff;
  138.     int blue  = (pixel      ) & 0xff;
  139.  
  140.     return (red == 255 && green == 255 && blue == 255) ? 0 : 1;
  141.   }
  142.  
  143.   private void loadImage(Image image)
  144.   {
  145.     synchronized(tracker)
  146.     {
  147.       tracker.addImage(image, 0);
  148.       try
  149.       {
  150.         tracker.waitForID(0, 0);
  151.       } catch (InterruptedException e) {
  152.         System.out.println("INTERRUPTED while loading Image");
  153.       }
  154.       tracker.statusID(0, false);
  155.       tracker.removeImage(image, 0);
  156.  
  157.       width = image.getWidth(imageObserver);
  158.       height = image.getHeight(imageObserver);
  159.     }
  160.   }
  161.  
  162.   /**
  163.    * Returns an array of pixels representing an image.
  164.    * @param image The image to get pixels from
  165.    */
  166.  
  167.   private int[] getPixels(Image image)
  168.   {
  169.     int[] pixels = new int[width * height];
  170.  
  171.     if (image != null)
  172.     {
  173.       try
  174.       {
  175.         PixelGrabber pg = new PixelGrabber(image, 0, 0, width, height, pixels, 0, width);
  176.         pg.grabPixels();
  177.         if ((pg.getStatus() & ImageObserver.ABORT) != 0)
  178.         {
  179.           System.out.println("#### ERROR");
  180.         }
  181.       } catch (InterruptedException ie) { }
  182.     }
  183.  
  184.     return pixels;
  185.   }
  186.  
  187.   /**
  188.    * Returns user directory.
  189.    */
  190.  
  191.   public static String getUserDirectory()
  192.   {
  193.     return System.getProperty("user.dir");
  194.   }
  195.  
  196.   /**
  197.    * When the user has to specify file names, he can use wildcards (*, ?). This methods
  198.    * handles the usage of these wildcards.
  199.    * @param path The path were to search
  200.    * @param s Wilcards
  201.    * @return An array of String which contains all files matching <code>s</code>
  202.    * in current directory.
  203.    */
  204.  
  205.   public static String[] getWildCardMatches(String path, String s)
  206.   {
  207.     if (s == null)
  208.       return null;
  209.  
  210.     String files[];
  211.     String filesThatMatch[];
  212.     String args = new String(s.trim());
  213.     Vector filesThatMatchVector = new Vector();
  214.  
  215.     if (path == null)
  216.       path = getUserDirectory();
  217.  
  218.     files = (new File(path)).list();
  219.     if (files == null)
  220.       return null;
  221.  
  222.     for (int i = 0; i < files.length; i++)
  223.     {
  224.       if (match(args, files[i]))
  225.       {
  226.         File temp = new File(getUserDirectory(), files[i]);
  227.         filesThatMatchVector.addElement(new String(temp.getName()));
  228.       }
  229.     }
  230.  
  231.     filesThatMatch = new String[filesThatMatchVector.size()];
  232.     filesThatMatchVector.copyInto(filesThatMatch);
  233.  
  234.     return filesThatMatch;
  235.   }
  236.  
  237.   /**
  238.    * This method can determine if a String matches a pattern of wildcards
  239.    * @param pattern The pattern used for comparison
  240.    * @param string The String to be checked
  241.    * @return true if <code>string</code> matches <code>pattern</code>
  242.    */
  243.  
  244.   public static boolean match(String pattern, String string)
  245.   {
  246.     for (int p = 0; ; p++)
  247.     {
  248.       for (int s = 0; ; p++, s++)
  249.       {
  250.         boolean sEnd = (s >= string.length());
  251.         boolean pEnd = (p >= pattern.length() || pattern.charAt(p) == '|');
  252.         if (sEnd && pEnd)
  253.           return true;
  254.         if (sEnd || pEnd)
  255.           break;
  256.         if (pattern.charAt(p) == '?')
  257.           continue;
  258.         if (pattern.charAt(p) == '*')
  259.         {
  260.           int i;
  261.           p++;
  262.           for (i = string.length(); i >= s; --i)
  263.             if (match(pattern.substring(p), string.substring(i))) return true;
  264.           break;
  265.         }
  266.         if (pattern.charAt(p) != string.charAt(s))
  267.           break;
  268.       }
  269.       p = pattern.indexOf('|', p);
  270.       if (p == -1)
  271.         return false;
  272.     }
  273.   }
  274.  
  275.   /**
  276.    * Quick sort an array of Strings.
  277.    * @param string Strings to be sorted
  278.    */
  279.  
  280.   public static void sortStrings(String[] strings)
  281.   {
  282.     sortStrings(strings, 0, strings.length - 1);
  283.   }
  284.  
  285.   /**
  286.    * Quick sort an array of Strings.
  287.    * @param a Strings to be sorted
  288.    * @param lo0 Lower bound
  289.    * @param hi0 Higher bound
  290.    */
  291.  
  292.   public static void sortStrings(String a[], int lo0, int hi0)
  293.   {
  294.     int lo = lo0;
  295.     int hi = hi0;
  296.     String mid;
  297.  
  298.     if (hi0 > lo0)
  299.     {
  300.       mid = a[(lo0 + hi0) / 2];
  301.  
  302.       while (lo <= hi)
  303.       {
  304.         while (lo < hi0 && a[lo].compareTo(mid) < 0)
  305.           ++lo;
  306.  
  307.         while (hi > lo0 && a[hi].compareTo(mid) > 0)
  308.           --hi;
  309.  
  310.         if (lo <= hi)
  311.         {
  312.           swap(a, lo, hi);
  313.           ++lo;
  314.           --hi;
  315.         }
  316.       }
  317.  
  318.       if (lo0 < hi)
  319.         sortStrings(a, lo0, hi);
  320.  
  321.       if (lo < hi0)
  322.         sortStrings(a, lo, hi0);
  323.     }
  324.   }
  325.  
  326.   /**
  327.    * Swaps two Strings.
  328.    * @param a The array to be swapped
  329.    * @param i First String index
  330.    * @param j Second String index
  331.    */
  332.  
  333.   public static void swap(String a[], int i, int j)
  334.   {
  335.     String T;
  336.     T = a[i];
  337.     a[i] = a[j];
  338.     a[j] = T;
  339.   }
  340. }
  341.  
  342. // End of Builder.java
  343.